Imports System.Text

Public Class SUM_Server
    Private Const Q As String = Chr(34)

    Private mlServerID As Long
    Private mlDatabaseID As Long = SQLAutoDocLib.Database.NODATA
    Private msBaseURL As String = ""

    Private mbShowBlocks As Boolean = False
    Private mbShowConnect As Boolean = False
    Private mbShowSize As Boolean = False
    Private mbShowWaitStates As Boolean = False
    Private mbShowSchema As Boolean = False

    Public Sub New( _
            ByVal sBaseURL As String, _
            ByVal lServerID As Long)

        msBaseURL = sBaseURL
        mlServerID = lServerID
    End Sub

    Public Property DatabaseID() As Long
        Get
            Return mlDatabaseID
        End Get
        Set(ByVal value As Long)
            mlDatabaseID = value
        End Set
    End Property

    Public Property ShowBlocks() As Boolean
        Get
            Return mbShowBlocks
        End Get
        Set(ByVal value As Boolean)
            mbShowBlocks = value
        End Set
    End Property

    Public Property ShowConnect() As Boolean
        Get
            Return mbShowConnect
        End Get
        Set(ByVal value As Boolean)
            mbShowConnect = value
        End Set
    End Property

    Public Property ShowSize() As Boolean
        Get
            Return mbShowSize
        End Get
        Set(ByVal value As Boolean)
            mbShowSize = value
        End Set
    End Property

    Public Property ShowWaitStates() As Boolean
        Get
            Return mbShowWaitStates
        End Get
        Set(ByVal value As Boolean)
            mbShowWaitStates = value
        End Set
    End Property

    Public Property ShowSchema() As Boolean
        Get
            Return mbShowSchema
        End Get
        Set(ByVal value As Boolean)
            mbShowSchema = value
        End Set
    End Property

    Public Function Summerize() As String

        Dim oServer As New SQLAutoDocLib.Server(mlServerID)
        oServer.Load()

        Dim oSum As New StringBuilder

        With oSum
            .Append("<table style=" & Q & "padding-top: 15px; padding-bottom: 5px; width: 500px; border-collapse: collapse; border: solid 1px black;" & Q & ">")
            .Append("<tr>")
            .Append("<td style=" & Q & "padding-top: 5px; vertical-align: bottom; text-align: left; padding-left: 10px; font-size: 14px; font-weight: bold;" & Q & ">")
            .Append(oServer.Name)
            .Append("</td>")
            .Append("</tr>")
            .Append("<tr>")
            .Append("<td style=" & Q & "vertical-align: bottom; text-align: left; padding-left: 20px; font-size: 10px; font-weight: normal;" & Q & ">")
            .Append(oServer.Desc)
            .Append("</td>")
            .Append("</tr>")

            If mlDatabaseID <> SQLAutoDocLib.Database.NODATA Then
                '---Show info for specific database
                .Append("<tr>")
                .Append("<td style=" & Q & "vertical-align: top; text-align: left;" & Q & ">")

                Dim osum_database As New SUM_Database(msBaseURL, mlDatabaseID)
                With osum_database
                    .ShowBlocks = mbShowBlocks
                    .ShowConnect = mbShowConnect
                    .ShowSchema = mbShowSchema
                    .ShowSize = mbShowSize
                    .ShowWaitStates = mbShowWaitStates
                End With
                .Append(osum_database.summerize)

                .Append("</td>")
                .Append("</tr>")
            Else
                '---Show info for all active databases on this server.
                Dim oFactory As New Database_Factory
                For Each oDatabase As SQLAutoDocLib.Database In oFactory.GetAllDatabasesForServer(mlServerID)
                    .Append("<tr>")
                    .Append("<td style=" & Q & "vertical-align: top; text-align: left;padding-left: 10px;" & Q & ">")

                    Dim osum_database As New SUM_Database(msBaseURL, oDatabase.ID)
                    With osum_database
                        .ShowBlocks = mbShowBlocks
                        .ShowConnect = mbShowConnect
                        .ShowSchema = mbShowSchema
                        .ShowSize = mbShowSize
                        .ShowWaitStates = mbShowWaitStates
                    End With
                    .Append(osum_database.summerize)

                    .Append("<br/>")

                    .Append("</td>")
                    .Append("</tr>")
                Next
            End If


            .Append("</table>")
        End With

        Return oSum.ToString
    End Function
End Class
